package org.farng.mp3.object;

import org.farng.mp3.TagUtility;


/**
 * <p>
 * Title:
 * </p>
 * 
 * <p>
 * Description:
 * </p>
 * 
 * <p>
 * Copyright: Copyright (c) 2002
 * </p>
 * 
 * <p>
 * Company:
 * </p>
 *
 * @author Eric Farng
 * @version $Revision: 1.8 $
 */
public class ObjectNumberFixedLength extends AbstractMP3Object {
    /** DOCUMENT ME! */
    int length = 0;

    /**
     * Creates a new ObjectNumberFixedLength object.
     *
     * @param identifier DOCUMENT ME!
     * @param size DOCUMENT ME!
     *
     * @throws IllegalArgumentException DOCUMENT ME!
     */
    public ObjectNumberFixedLength(String identifier, int size) {
        if (size < 0) {
            throw new IllegalArgumentException("Length is less than zero: " + length);
        }

        this.length     = size;
        this.identifier = identifier;
    }

    public ObjectNumberFixedLength(ObjectNumberFixedLength copyObject) {
        super(copyObject);
        this.length = copyObject.length;
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public int getLength() {
        return length;
    }

    /**
     * DOCUMENT ME!
     *
     * @param length DOCUMENT ME!
     */
    public void setSize(int length) {
        if (length > 0) {
            this.length = length;
        }
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public int getSize() {
        return length;
    }

    /**
     * DOCUMENT ME!
     *
     * @param obj DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public boolean equals(Object obj) {
        if ((obj instanceof ObjectNumberFixedLength) == false) {
            return false;
        }

        ObjectNumberFixedLength object = (ObjectNumberFixedLength) obj;

        if (this.length != object.length) {
            return false;
        }

        return super.equals(obj);
    }

    /**
     * DOCUMENT ME!
     *
     * @param arr DOCUMENT ME!
     * @param offset DOCUMENT ME!
     *
     * @throws NullPointerException DOCUMENT ME!
     * @throws IndexOutOfBoundsException DOCUMENT ME!
     */
    public void readByteArray(byte[] arr, int offset) {
        if (arr == null) {
            throw new NullPointerException("Byte array is null");
        }

        if ((offset < 0) || (offset >= arr.length)) {
            throw new IndexOutOfBoundsException("Offset to byte array is out of bounds: offset = " + offset + ", array.length = " + arr.length);
        }

        long lvalue = 0;

        for (int i = offset; i < (offset + length); i++) {
            lvalue <<= 8;
            lvalue += arr[i];
        }

        value = new Long(lvalue);
    }

    /**
     * DOCUMENT ME!
     *
     * @param str DOCUMENT ME!
     * @param offset DOCUMENT ME!
     *
     * @throws NullPointerException DOCUMENT ME!
     * @throws IndexOutOfBoundsException DOCUMENT ME!
     */
    public void readString(String str, int offset) {
        if (str == null) {
            throw new NullPointerException("Number string is null");
        }

        if ((offset < 0) || (offset >= str.length())) {
            throw new IndexOutOfBoundsException("Offset to number string is out of bounds: offset = " + offset + ", string.length()" + str.length());
        }

        value = Long.getLong(str.substring(offset));
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public String toString() {
        if (value == null) {
            return "";
        } else {
            return value.toString();
        }
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public byte[] writeByteArray() {
        byte[] arr;

        arr = new byte[length];

        if (value != null) {
            long temp = TagUtility.getWholeNumber(value);

            for (int i = length - 1; i >= 0; i--) {
                arr[i] = (byte) (temp & 0xFF);
                temp >>= 8;
            }
        }

        return arr;
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public String writeString() {
        if (value == null) {
            return String.valueOf(new char[length]);
        } else {
            return value.toString();
        }
    }
}